import {getOrderDetail} from "@src/api/deposit";
import dayjs from "dayjs";
import router from "@src/router";

Component({
  data: {
    orderCode: '',
    order: null,
  },
  methods: {
    onLoad(options) {
      this.setData({orderCode: options.orderCode});
    },
    onLogin() {
      this.fetchData();
    },
    onPullDownRefresh() {
      wx.stopPullDownRefresh();
      this.fetchData();
    },
    fetchData() {
      let data = {
        orderCode: this.data.orderCode
      };
      getOrderDetail(data).then(res => {
        if (res.code === 0) {
          //处理数据
          let order = res.data.order
          if (order.status === 0) {
            order.statusTitle = "已寄存";
            order.subStatusTitle = "您的物品已寄存成功，记得按时支取哦~";
            let day = dayjs(order.expireTime).diff(dayjs(), "day") + 1;
            if (day <= 7) {
              order.isNearExpire = true;
            } else {
              order.isNearExpire = false;
            }
          } else if (order.status === 1) {
            order.statusTitle = "已支取";
            order.subStatusTitle ="您寄存的物品已支取，期待您的再次使用寄存功能";
          } else if (order.status === 2) {
            order.statusTitle = "已过期";
            order.subStatusTitle = "您寄存的物品已过期，如果有疑问请联系商家处理.";
          }
          if (order.depositInfo) {
            order.depositInfo = JSON.parse(order.depositInfo);
          }
          this.setData({order: order, reqGoodsData: res.data.goods})
        }
      })
    },
    handleShowWithdraw() {
      this.selectComponent("#withdrawDialog").open();
    },
    handleShowTracking() {
      this.selectComponent("#trackDialog").open();
    },
    handleReStore() {
      router.push({
        name: 'deposit/reStore',
        params: {orderCode: this.data.orderCode},
        events: {
          'renew': () => {
            this.fetchData();
          }
        }
      })
    },
    handleTake() {
      this.selectComponent("#takeDialog").open(this.data.orderCode).then(res => {
        this.fetchData();
      });
    }
  }
})
